package com.alm.token.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alm.token.ConStant;
import com.alm.token.MyApp;
import com.alm.token.R;
import com.alm.token.base.BaseActivity;
import com.alm.token.ui.data.bean.BindEmailData;
import com.alm.token.ui.data.bean.RegisterData;
import com.alm.token.ui.persenter.MyPreenter;
import com.alm.token.ui.view.BindEmailView;
import com.alm.token.utils.ActionBarHelper;
import com.alm.token.utils.EmptyUtils;
import com.alm.token.utils.LanguageUtils;
import com.alm.token.utils.LogUtil;
import com.alm.token.utils.StringUtils;
import com.alm.token.widget.Countdownbutton;

import org.greenrobot.eventbus.EventBus;

import baibeiwallet.event.RefreshMeEvent;
import baibeiwallet.utils.DialogUtils;
import butterknife.BindView;
import butterknife.ButterKnife;

import static com.alm.token.ui.activity.FundArealistActivity.INTENT_RESULT_CODE_VAULE;
import static com.alm.token.ui.activity.FundArealistActivity.INTENT_RESULT_INTO_CODE;
import static com.alm.token.ui.activity.FundArealistActivity.INTENT_RESULT_NAME_VAULE;
import static com.alm.token.ui.activity.FundArealistActivity.INTENT_RESULT_SUCC_CODE;
import static com.alm.token.ui.activity.MnemonicActivity.INTENT_Mnemonic;
import static com.alm.token.ui.activity.MnemonicActivity.INTENT_Mnemonic_INTO_CODE;
import static com.alm.token.ui.activity.MnemonicActivity.INTENT_Mnemonic_RESULT_SUCC_CODE;
import static com.alm.token.ui.persenter.MyPreenter.bind_mail;
import static com.alm.token.ui.persenter.MyPreenter.change_mail;

public class BindEmaiActivity extends BaseActivity implements BindEmailView<RegisterData> {


    @BindView(R.id.tv_clickphonecountry)
    TextView tvClickphonecountry;
    @BindView(R.id.edt_inputemail)
    EditText edtInputemail;
    @BindView(R.id.edt_inputemaicode)
    EditText edtInputemaicode;
    @BindView(R.id.tv_getcode)
    Countdownbutton tvGetcode;
    @BindView(R.id.tv_registersubmit)
    TextView tvRegistersubmit;
    @BindView(R.id.ly_bindmail_body)
    LinearLayout lyBindmailBody;
    @BindView(R.id.edt_inputeoldmail)
    TextView edtInputeoldmail;
    @BindView(R.id.edt_inputoldemaicode)
    EditText edtInputoldemaicode;
    @BindView(R.id.tv_getoldcode)
    Countdownbutton tvGetoldcode;
    @BindView(R.id.edt_inputenewmail)
    EditText edtInputenewmail;
    @BindView(R.id.edt_inputnewemaicode)
    EditText edtInputnewemaicode;
    @BindView(R.id.tv_getnewcode)
    Countdownbutton tvGetnewcode;
    @BindView(R.id.ly_upmail_body)
    LinearLayout lyUpmailBody;

    @BindView(R.id.ly_upmail_oldbody)
    LinearLayout ly_upmail_oldbody;
    @BindView(R.id.ly_upmail_newbody)
    LinearLayout ly_upmail_newbody;
    @BindView(R.id.tv_registersubmit_back)
    TextView tv_registersubmit_back;

    private String strCountry, strAreacode, strEmail, strCode, strnewEmail, strnewCode, stroldEmail, stroldCode;

    public static final int BINDMAI_bind = 1, BINDMAI_up = 2;

    private int type = BINDMAI_bind;

    @Override
    protected MyPreenter initPreenter() {
//        RetrofitUtil.getInstance().setInstance();
//        MyApp.mOkHttpClient=null;
        return preenter = new MyPreenter(this);
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_bind_emai;
    }

    @Override
    protected void initVariables() {

        if (LanguageUtils.getSetIndex() == 1) {
            //中文  name=中国  code=86
            tvClickphonecountry.setTag("86");
            tvClickphonecountry.setText("中国");
        } else if (LanguageUtils.getSetIndex() == 3) {
            //韩文  name=韩国  code=82
            tvClickphonecountry.setTag("82");
            tvClickphonecountry.setText("Korea");
        } else if (LanguageUtils.getSetIndex() == 4) {
            //日文 name=日本  code=81
            tvClickphonecountry.setTag("861");
            tvClickphonecountry.setText("Japan");
        } else {
            //英文 name=美国  code=1
            tvClickphonecountry.setTag("1");
            tvClickphonecountry.setText("USA");
        }
        type = getIntent().getIntExtra("BINDMAI_type", BINDMAI_bind);
    }

    @Override
    protected void initViews() {
        new ActionBarHelper(this).setTitle(
                getString(type == BINDMAI_bind ? R.string.soft_hint_email : R.string.soft_hint_upemail));
//        tvRegistersubmit.setText(getString(type == BINDMAI_bind ? R.string.tv_hint_next : R.string.crowdfund_hint_crowdfundsure));
        edtInputeoldmail.setText(StringUtils.nullToStr(MyApp.getMail()));
        if (MyApp.getMail() != null) {
            String[] split = MyApp.getMail().split("@");
            edtInputeoldmail.setText(split[0].length() > 4 ? split[0].substring(0, 2) + "****" + split[0].substring(split[0].length() - 2, split[0].length()) + "@" + split[1] : MyApp.getMail());
        }

        lyBindmailBody.setVisibility(type == BINDMAI_bind ? View.VISIBLE : View.GONE);
        lyUpmailBody.setVisibility(type == BINDMAI_bind ? View.GONE : View.VISIBLE);
        ly_upmail_newbody.setVisibility(View.GONE);
        tvRegistersubmit.setOnClickListener(this);
        tv_registersubmit_back.setOnClickListener(this);
        tvGetcode.setOnClickListener(this);
        tvClickphonecountry.setOnClickListener(this);

        tvGetnewcode.setOnClickListener(this);
        tvGetoldcode.setOnClickListener(this);
    }

    @Override
    protected void loadData() {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_clickphonecountry:
//                showDownPop(tvSetphonecode);
                startActivityForResult(FundArealistActivity.class, INTENT_RESULT_INTO_CODE);
                break;

            case R.id.tv_getcode:
                strEmail = edtInputemail.getText().toString();
                if (strEmail.isEmpty()) {
                    showToast(getString(R.string.email_hint_iputemail));
                    edtInputemail.setFocusable(true);
                    edtInputemail.setFocusableInTouchMode(true);
                    edtInputemail.requestFocus();
                    //显示软键盘
//                     this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
                    //如果上面的代码没有弹出软键盘 可以使用下面另一种方式
                    InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.showSoftInput(edtInputemail, 0);
                    break;
                }
                tvGetcode.start();
                preenter.sendMailCode(strEmail, bind_mail);
                break;

            case R.id.tv_getnewcode:
                strnewEmail = edtInputenewmail.getText().toString();
                if (strnewEmail.isEmpty()) {
                    showToast(getString(R.string.email_hint_iputemail));
                    edtInputenewmail.setFocusable(true);
                    edtInputenewmail.setFocusableInTouchMode(true);
                    edtInputenewmail.requestFocus();
                    //显示软键盘
//                     this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
                    //如果上面的代码没有弹出软键盘 可以使用下面另一种方式
                    InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.showSoftInput(edtInputenewmail, 0);
                    break;
                }
                tvGetnewcode.start();
                preenter.sendMailCode(strnewEmail, change_mail);
                break;

            case R.id.tv_getoldcode:
                stroldEmail = edtInputeoldmail.getText().toString();
                if (stroldEmail.isEmpty()) {
                    showToast(getString(R.string.email_hint_iputemail));
                    edtInputeoldmail.setFocusable(true);
                    edtInputeoldmail.setFocusableInTouchMode(true);
                    edtInputeoldmail.requestFocus();
                    //显示软键盘
//                     this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
                    //如果上面的代码没有弹出软键盘 可以使用下面另一种方式
                    InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.showSoftInput(edtInputeoldmail, 0);
                    break;
                }
                tvGetoldcode.start();
                preenter.sendMailCode(MyApp.getMail(), change_mail);
                break;
            case R.id.tv_registersubmit:
                if (type == BINDMAI_bind) {
                    if (edtInputemail.getText().toString().isEmpty()) {
                        showToast(getString(R.string.email_hint_iputemail));
                        edtInputemail.setFocusable(true);
                        edtInputemail.setFocusableInTouchMode(true);
                        edtInputemail.requestFocus();
                        //显示软键盘
//                     this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
                        //如果上面的代码没有弹出软键盘 可以使用下面另一种方式
                        InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
                        imm.showSoftInput(edtInputemail, 0);
                        break;
                    }
                    if (edtInputemaicode.getText().toString().isEmpty()) {
                        showToast(getString(R.string.email_hint_iputemailcode));
                        edtInputemaicode.setFocusable(true);
                        edtInputemaicode.setFocusableInTouchMode(true);
                        edtInputemaicode.requestFocus();
                        //显示软键盘
//                     this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
                        //如果上面的代码没有弹出软键盘 可以使用下面另一种方式
                        InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
                        imm.showSoftInput(edtInputemaicode, 0);
                        break;
                    }

                    strCountry = tvClickphonecountry.getText().toString();
                    strAreacode = (String) tvClickphonecountry.getTag();
                    strEmail = edtInputemail.getText().toString();
                    strCode = edtInputemaicode.getText().toString();


                    if (EmptyUtils.isNotEmpty(MyApp.getUserData())
                            && EmptyUtils.isNotEmpty(MyApp.getUserData().getData())
                            && MyApp.getUserData().getData().isBindMnemonic()) {
                        if (MyApp.getUserData().getData().isPayPwd()) {

                            DialogUtils.enterPayPsw(this, new DialogUtils.IEnterPwdListener() {
                                @Override
                                public void onSuccess(String pwd) {
                                    preenter.bindMail(strEmail, strCode, bind_mail, "", pwd, strCountry, strAreacode);
                                }
                            });

                        } else {
                            preenter.bindMail(strEmail, strCode, bind_mail, "", "", strCountry, strAreacode);
                        }
                    } else {
                        if (EmptyUtils.isNotEmpty(MyApp.getUserData())
                                && EmptyUtils.isNotEmpty(MyApp.getUserData().getData())
                                && MyApp.getUserData().getData().isPayPwd()) {

                            DialogUtils.enterPayPsw(this, new DialogUtils.IEnterPwdListener() {
                                @Override
                                public void onSuccess(String pwd) {
                                    preenter.verifyCode(strEmail, strCode, "0", pwd);
                                }
                            });

                        } else {
                            preenter.verifyCode(strEmail, strCode, "0", "");
                        }
                    }
                } else {


                    if (ly_upmail_newbody.isShown()) {

                        if (edtInputenewmail.getText().toString().isEmpty()) {
                            showToast(getString(R.string.email_hint_iputemail));
                            edtInputenewmail.setFocusable(true);
                            edtInputenewmail.setFocusableInTouchMode(true);
                            edtInputenewmail.requestFocus();
                            //显示软键盘
//                     this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
                            //如果上面的代码没有弹出软键盘 可以使用下面另一种方式
                            InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
                            imm.showSoftInput(edtInputenewmail, 0);
                            break;
                        }
                        if (edtInputnewemaicode.getText().toString().isEmpty()) {
                            showToast(getString(R.string.email_hint_iputemailcode));
                            edtInputnewemaicode.setFocusable(true);
                            edtInputnewemaicode.setFocusableInTouchMode(true);
                            edtInputnewemaicode.requestFocus();
                            //显示软键盘
//                     this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
                            //如果上面的代码没有弹出软键盘 可以使用下面另一种方式
                            InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
                            imm.showSoftInput(edtInputnewemaicode, 0);
                            break;
                        }
                        strnewEmail = edtInputenewmail.getText().toString();
                        stroldEmail = edtInputeoldmail.getText().toString();
                        strnewCode = edtInputnewemaicode.getText().toString();
                        stroldCode = edtInputoldemaicode.getText().toString();
                        preenter.upMail(MyApp.getMail(), stroldCode, strnewEmail, strnewCode);
                    } else {
                        if (edtInputeoldmail.getText().toString().isEmpty()) {
                            showToast(getString(R.string.email_hint_iputemail));
                            edtInputeoldmail.setFocusable(true);
                            edtInputeoldmail.setFocusableInTouchMode(true);
                            edtInputeoldmail.requestFocus();
                            //显示软键盘
//                     this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
                            //如果上面的代码没有弹出软键盘 可以使用下面另一种方式
                            InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
                            imm.showSoftInput(edtInputeoldmail, 0);
                            break;
                        }
                        if (edtInputoldemaicode.getText().toString().isEmpty()) {
                            showToast(getString(R.string.email_hint_iputemailcode));
                            edtInputoldemaicode.setFocusable(true);
                            edtInputoldemaicode.setFocusableInTouchMode(true);
                            edtInputoldemaicode.requestFocus();
                            //显示软键盘
//                     this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
                            //如果上面的代码没有弹出软键盘 可以使用下面另一种方式
                            InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
                            imm.showSoftInput(edtInputoldemaicode, 0);
                            break;
                        }

                        ly_upmail_oldbody.setVisibility(View.GONE);
                        ly_upmail_newbody.setVisibility(View.VISIBLE);
                        tvRegistersubmit.setText(getString(R.string.crowdfund_hint_crowdfundsure));
                        tv_registersubmit_back.setVisibility(View.VISIBLE);

//                        if (EmptyUtils.isNotEmpty(MyApp.getUserData())
//                                && EmptyUtils.isNotEmpty(MyApp.getUserData().getData())
//                                && MyApp.getUserData().getData().isPayPwd()) {
//
//                            DialogUtils.enterPayPsw(this, new DialogUtils.IEnterPwdListener() {
//                                @Override
//                                public void onSuccess(String pwd) {
//                                    preenter.verifyCode(MyApp.getMail(), edtInputoldemaicode.getText().toString(), "0", pwd);
//                                }
//                            });
//
//                        } else {
//                            preenter.verifyCode(MyApp.getMail(), edtInputoldemaicode.getText().toString(), "0", "");
//                        }
                    }
                }

                break;
            case R.id.tv_registersubmit_back:
                ly_upmail_newbody.setVisibility(View.GONE);
                tv_registersubmit_back.setVisibility(View.GONE);
                ly_upmail_oldbody.setVisibility(View.VISIBLE);
                tvRegistersubmit.setText(getString(R.string.tv_hint_next));
                break;
            case R.id.tv_termsofservice:
//                showToast("点击获取服务条款");
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == INTENT_RESULT_INTO_CODE && resultCode == INTENT_RESULT_SUCC_CODE && data != null) {
            String name = data.getStringExtra(INTENT_RESULT_NAME_VAULE);
            String code = data.getStringExtra(INTENT_RESULT_CODE_VAULE);
            LogUtil.e("name=" + name + "  code=" + code);

            tvClickphonecountry.setTag(code);
            tvClickphonecountry.setText(name);
        } else if (requestCode == INTENT_Mnemonic_INTO_CODE && resultCode == INTENT_Mnemonic_RESULT_SUCC_CODE && data != null) {
            String monic = data.getStringExtra(INTENT_Mnemonic);

            if (EmptyUtils.isNotEmpty(MyApp.getUserData()) && EmptyUtils.isNotEmpty(MyApp.getUserData().getData()) && MyApp.getUserData().getData().isPayPwd()) {

                DialogUtils.enterPayPsw(this, new DialogUtils.IEnterPwdListener() {
                    @Override
                    public void onSuccess(String pwd) {
                        preenter.bindMail(strEmail, strCode, bind_mail, monic, pwd, strCountry, strAreacode);
                    }
                });

            } else {
                preenter.bindMail(strEmail, strCode, bind_mail, monic, "", strCountry, strAreacode);
            }

        }
    }

    @Override
    public void getData(RegisterData registerData) {
        if (registerData.getStatus() == 0) {

//            if(type==BINDMAI_bind){
            startActivityForResult(MnemonicActivity.class, INTENT_Mnemonic_INTO_CODE);
//            }else {
//                ly_upmail_oldbody.setVisibility(View.GONE);
//                ly_upmail_newbody.setVisibility(View.VISIBLE);
//                tvRegistersubmit.setText(getString(R.string.crowdfund_hint_crowdfundsure));
//                tv_registersubmit_back.setVisibility(View.VISIBLE);
//            }
        } else {
            showToast(registerData.getMsg());
        }
    }

    @Override
    public void sendMailCode(RegisterData registerData) {
        showToast(registerData.getMsg());
    }

    @Override
    public void upMail(RegisterData registerData) {
        showToast(registerData.getMsg());
        if (registerData.getStatus() == 0) {
            EventBus.getDefault().post(new RefreshMeEvent(1));
            finish();
        }
    }

    @Override
    public void bindMail(BindEmailData bindEmailData) {
        showToast(bindEmailData.getMsg());
        if (bindEmailData.getStatus() == 0) {
            EventBus.getDefault().post(new RefreshMeEvent(1));
            finish();
        }
    }

    @Override
    public void jumpToLoginAct(int staue) {
        if (staue == 3) {
//            showToast(getString(R.string.common_relogin));
//            Intent intent = new Intent();
//            intent.setAction(ConStant.EXIT_APP);
//            sendBroadcast(intent);
//            startActivity(BlockWalletLoginActivity.class);
//            finish();
        } else if (staue == 4) {
//            showToast(getString(R.string.paypasw_hint_setpaypsw));
//            Intent intent = new Intent(this,PayPwdActivity.class);
//            intent.putExtra("PayPwdActivityType",PayPwdActivity.PAYPSW_SET);
//            startActivity(intent);
//            finish();
            showToSetPaypswDialog();
        } else if (staue == 666) {
            showToUpdata();
        }
    }

    @Override
    public void onError(String error) {
        showToast(getString(R.string.common_loaderrritry));
    }

    @Override
    public void ShowProgress() {
        showWaitDialog(getString(R.string.profile_hint_warn17));
    }

    @Override
    public void hideProgress() {
        hideWaitDialog();
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }


}
